package com.timedomain.him.remake.core;
import android.content.Context;
import android.content.res.AssetManager;

import com.timedomain.him.remake.Program;
import com.timedomain.him.remake.utils.DataPackManager;
import com.timedomain.him.remake.utils.HLog;

import static com.un4seen.bass.BASS.*;

public class ChannelCreator {

    public static int FromFile(Context ctx,String path){
        HLog.d("ChannelCreator", "Open: "+path);
        DataPackManager.DataPackEntry dpe = Program.getDataPack(ctx).get(path);
        if(dpe != null){
            if(dpe.prefix.startsWith("@")){
                return FromAsset(ctx,dpe);
            }
            else if(dpe.prefix.startsWith("~")){
                return FromDiskFile(dpe);
            }
        }
        return 0;
    }
    public static int FromAsset(Context ctx, DataPackManager.DataPackEntry entry){
        AssetManager assetManager = ctx.getAssets();
        String path = "data/"+ entry.hash;
        HLog.d("ChannelCreator", "FromFile: "+entry.path+"->"+path);
        int handle = BASS_StreamCreateFile(new Asset(assetManager,path),0,0,0);
        int error = BASS_ErrorGetCode();
        if(error != BASS_OK){
            HLog.w("Bass create channel error,code=",""+error);
        }

        return handle;
    }
    public static int FromAsset(AssetManager am, String absolutePath){

        int handle = BASS_StreamCreateFile(new Asset(am,absolutePath),0,0,0);
        int error = BASS_ErrorGetCode();
        if(error != BASS_OK){
            HLog.w("Bass create channel error,code=",""+error);
        }

        return handle;
    }


    public static int FromDiskFile(DataPackManager.DataPackEntry entry){
        HLog.d("ChannelCreator", "FromFile: "+entry.path+"->"+entry.hash);
        int handle = BASS_StreamCreateFile(entry.hash,0,0,0);
        int error = BASS_ErrorGetCode();
        if(error != BASS_OK){
            HLog.w("Bass create channel error,code=",""+error);
        }
        return handle;
    }
}
